#1/usr/bin/env python
import time
import smbus

#********************************************
# Custard Pi 7 resources v1.0 5th Dec 2013

#I2C addresses
#Set suitable address on Custard Pi 7

add0= 0x20
add1= 0x21
add2= 0x22
add3= 0x23
add4= 0x24
add5= 0x25
add6= 0x26
add7= 0x27

bus=smbus.SMBus(1)

#set IODIR register 
iodir= 0x00
#set bit 0 to 3 as inputs,bits 4 to 7 as outputs
inout= 0x0F
#set GPIO register
gpio= 0x09
#set output latch
olat=0x0A

#set output HIGH
ONbit4= 0x10
ONbit5= 0x20
ONbit6= 0x40
ONbit7= 0x80

#set output LOW
OFFbit4= 0xEF
OFFbit5= 0xDF
OFFbit6= 0xBF
OFFbit7= 0x7F

def setbit(address, byte):
    #sets selected port pin
    outstatus = bus.read_byte_data(address, olat) | byte
    bus.write_byte_data(address, gpio, outstatus)

def clrbit(address, byte):
    #clears selected port pin
    outstatus = bus.read_byte_data(address, olat) & byte
    bus.write_byte_data (address, gpio, outstatus)

def readbit(address, bit):
    #read status of bit 0 to 3
    bitvalue = bus.read_byte_data(address, gpio) & bit
    return bitvalue

def setinandout(address):            
    #set inputs & outputs
    bus.write_byte_data(address, iodir, inout)

def setpullups(address):            
    #set inputs & outputs
    bus.write_byte_data(address, 0x06, 0xFF)

def alloff(address):
    #clear all outputs low
    bus.write_byte_data (address, gpio, 0x00)

#******************************************